<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"  xmlns="http://www.w3.org/1999/xhtml">
	<head th:replace="_fragments :: head(~{::title})">
		<meta name="viewport" content="width=device-width,initial-scale=1.0">
		<meta charset="utf-8" />
		<title>留言板</title>
		<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
		<link rel="stylesheet"  href="../static/css/me.css" />
	</head>
	<body id="comment-container">
		<!--导航-->
		<nav  th:replace="_fragments :: menu(6)" class="ui inverted attached segment m-padded-tb-mini" >
			<div class="ui container" >
				<div class="ui inverted secondary stackable menu">
					<h2 class="ui teal header item">Blog</h2>
					<a href="#" class="m-item item m-mobile-hide"><i class="mini home icon"></i>首页</a>
					<a href="#" class="m-item item  m-mobile-hide"><i class="mini idea icon"></i>分类</a>
					<a href="#" class="m-item item  m-mobile-hide"><i class="mini tags icon"></i>标签</a>
					<a href="#" class="m-item item m-mobile-hide"><i class="mini clone icon"></i>归档</a>
					<a href="#" class="m-item item  m-mobile-hide"><i class="mini info icon"></i>关于我</a>
					<div class="right m-item item m-mobile-hide">
						<div class="ui icon inverted transparent input">
							<input type="text" placeholder="点此搜索"/>
							<i class="search link icon"></i>
						</div>
					</div>
				</div>
			</div>
			<a href="#" class="ui menu toggle black icon button m-right-top m-mobile-show">
				<i class="sidebar icon"></i>
			</a>
		</nav>
		<!--中间内容-->
		<div class="m-container m-padded-tb-big">
			<div class="ui container">
				<div class="ui top attached segment">
					<div class="ui middle aligned column grid">
						<div class="column">
							<h3 class="ui teal header">留言</h3>
						</div>
					</div>
				</div>
				<div class="ui bottom attached segment">
					<!--留言区域-->
					<div id="comment-form" class="ui form">
						<input type="hidden" name="parentMessage.id" value="-1">
						<div class="field">
							<textarea name="content" placeholder="请输入留言信息...."></textarea>
						</div>
						<div class="fields">
							<div class="field m-mobile-wide m-margin-bottom-small">
								<div class="ui left icon input">
									<i class="user icon"></i>
									<input type="text" name="nickname" placeholder="姓名" th:value="${session.user}!=null ? ${session.user.nickname} : '匿名游客'"/>
								</div>
							</div>
							<div class="field m-mobile-wide m-margin-bottom-small">
								<div class="ui left icon input">
									<i class="mail icon"></i>
									<input type="text" name="email" placeholder="邮箱" th:value="${session.user}!=null ? ${session.user.email}"/>
								</div>
							</div>
							<div class="field m-mobile-wide m-margin-bottom-small">
								<button id="commentpost-btn" type="button" class="ui teal button m-mobile-wide"><i class="edit icon"></i>发布</button>
							</div>
						</div>
					</div>
					<div class="ui  container">
						<div class="ui warning message" th:if="${message != null}">
							<div class="header">提示:</div>
							<p th:text="${message}">恭喜，操作成功</p>
						</div>
					</div>
					<div  class="ui teal segment">
							<div class="ui threaded comments" style="max-width: 100%;" >
								<h3 class="ui dividing header">留言内容</h3>
								<div class="comment" th:each="message : ${page.list}">
									<a class="avatar">
										<img src="https://picsum.photos/id/1/100/100?image=129" th:src="@{${message.avatar}}">
									</a>
									<div class="content">
										<a class="author">
											<span th:text="${message.nickname}"></span>
											<div class="ui mini basic teal leaf pointing label m-padded-mini" th:if="${message.adminReply}">博主</div>
										</a>
										<div class="metadata">
											<span class="date" th:text="${#dates.format(message.createTime,'yyyy-MM-dd HH:mm')}"></span>
										</div>
										<div class="text" th:text="${message.content}">太赞了！ </div>
										<div class="actions">
											<a class="reply" data-commentid="1" data-commentnickname="Matt" th:attr="data-commentid=${message.id},data-commentnickname=${message.nickname}" onclick="reply(this)">回复</a>
										</div>
									</div>
									<div class="comments" th:if="${#arrays.length(message.replyMessage)}>0">
										<div class="comment" th:each="reply : ${message.replyMessage}">
											<a class="avatar">
												<img src="https://picsum.photos/id/1/100/100?image=129"th:src="@{${reply.avatar}}">
											</a>
											<div class="content">
												<a class="author" >
													<span th:text="${reply.nickname}"></span>
													<!--th:text="${reply.comment.nickname}"-->
													<div class="ui mini basic teal leaf pointing label m-padded-mini" th:if="${reply.adminReply}">博主</div>
													&nbsp;<span th:text="|@ ${message.nickname}|"  class="m-teal">@ 小白</span>
												</a>
												<div class="metadata">
													<span class="date" th:text="${#dates.format(reply.createTime,'yyyy-MM-dd HH:mm')}">今天下午 5:42</span>
												</div>
												<div class="text" th:text="${reply.content}">太赞了！ </div>
												<div class="actions">
													<a class="reply" data-commentid="1" data-commentnickname="Matt" th:attr="data-commentid=${reply.id},data-commentnickname=${reply.nickname}" onclick="reply(this)">回复</a>
											</div>
											</div>
										</div>
									</div>
								</div>
							</div>
						<div class="ui bottom attached segment">
							<div class="ui middle aligned two column grid">
								<div class="column">
									<a class="ui primary basic button" th:href="@{/messages(page=${page.prePage})}">上一页</a>
								</div>
								<div class="right aligned column">
									<a class="ui primary basic button" th:href="@{/messages(page=${page.nextPage})}">下一页</a>
								</div>
							</div>
						</div>
					</div>

		</div>
		  <br />
	    	<br />
	    	<br />
	    	<br />
	    	<br />
	    	<br />
	    	<br />
			</div>
		</div>
		<!--底部footer-->
	    <footer th:replace="_fragments :: footer" class="ui inverted vertical segment m-padded-tb-massive">
	    </footer>
		<!--/*/<th:block th:replace="_fragments :: script">/*/-->
		<script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.js">
		</script>
		<script src="https://cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js"></script>
		<!--/*/</th:block>/*/-->
		<script>
			$('.menu.toggle').click(function(){
				$('.m-item').toggleClass('m-mobile-hide');
			});
            $('.message .close')
                .on('click',function () {
                    $(this)
                        .closest('.message')
                        .transition('fade');
                });
            $('.ui.form').form({
                fields: {
                    content: {
                        identifier: 'content',
                        rules: [{
                            type: 'empty',
                            prompt: '标题:请输入留言的内容'
                        }]
                    },
                    email: {
                        identifier: 'email',
                        rules: [{
                            type: 'email',
                            prompt: '标题:请输入邮箱地址'
                        }]

                    },
                    nickname : {
                        identifier: 'nickname',
                        rules: [{
                            type : 'empty',
                            prompt: '标题：请输入你的大名'
                        }]
                    }
                }
            });
            $('#commentpost-btn').click(function () {
                var boo =  $('.ui.form').form('validate form');
                if (boo){
                    console.log("校验成功");
                    postData();
                } else {
                    console.log("校验失败");
                }
            });
            function postData() {
                $("#comment-container").load(/*[[@{/messages}]]*/"",{
                    "parentMessage.id" : $("[name='parentMessage.id']").val(),
                    "nickname": $("[name='nickname']").val(),
                    "email"   : $("[name='email']").val(),
                    "content" : $("[name='content']").val()
                },function (responseTxt, statusTxt, xhr) {
                    //        $(window).scrollTo($('#comment-container'),500);
                    clearContent();
                });
            }
            function clearContent() {
                $("[name='content']").val('');
                $("[name='parentMessage.id']").val(-1);
                $("[name='content']").attr("placeholder", "请输入留言内容...");
            }

            function reply(obj) {
                var commentId = $(obj).data('commentid');
                var commentNickname = $(obj).data('commentnickname');
                $("[name='content']").attr("placeholder", "@"+commentNickname).focus();
                $("[name='parentMessage.id']").val(commentId);
                $(window).scrollTo($('#comment-form'),500);
            }
		</script>
	</body>
</html>
